package cn.edu.scau.cmi.xuxingao.client;

import java.util.List;

import cn.edu.scau.cmi.xuxingao.singleton.Chairman;
import cn.edu.scau.cmi.xuxingao.singleton.Marshal;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SingletonClient extends Application {

	@Override
	public void start(Stage primaryStage) {
		Group root = new Group();
		TextField inText = new TextField();
		Button createSingle = new Button("单例模式创建");
		Button createMultiple = new Button("多例模式创建");
		HBox hbox = new HBox();
		Label textLabel = new Label();

		VBox vbox = new VBox();
		hbox.getChildren().add(inText);
		hbox.getChildren().add(createSingle);
		hbox.getChildren().add(createMultiple);
		vbox.getChildren().addAll(hbox, textLabel);
		root.getChildren().addAll(vbox);
		
		Scene scene = new Scene(root, 500, 500);
		primaryStage.setScene(scene);
		primaryStage.show();
		
		createSingle.setOnAction(new EventHandler<ActionEvent>() {
			String info="";
			public void handle(ActionEvent event) {
				if(Chairman.check()) {
					Chairman instance = Chairman.getInstance(inText.getText());
					textLabel.setText(info += (instance.getName()+instance+'\n'));
				}
				else {
					Chairman instance = Chairman.getInstance(inText.getText());
					textLabel.setText(info += ("已经有一个主席了，他是"+instance.getName()+instance+'\n'));
				}
			}
		});
		
		createMultiple.setOnAction(new EventHandler<ActionEvent>() {
			String info="";
			public void handle(ActionEvent event) {
				if(Marshal.check()) {
					List<Marshal> marshals = Marshal.createInstance(inText.getText());
					int i = marshals.size();
					textLabel.setText(info += ("第"+i+"个元帅"+marshals.get(i-1).getName()+marshals.get(i-1)+'\n'));
				}
				else {
					List<Marshal> marshals = Marshal.createInstance(inText.getText());
					textLabel.setText(info += ("已经有十位元帅，无法任命更多元帅！\n"));
				}
				

			}
		});
	}
	
	public static void main(String[] args) {
		
		launch(args);
		
	}

}